package com.sse.task.runner;

import com.sse.task.ScheduledPushServer;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @title: LivePushRunner
 * @Author xu-xiang
 * @Date: 2024/1/26 16:09
 * @description:  测试项目启动后事实推送数据到前端
 */
@Component
@ConditionalOnBean(ScheduledPushServer.class)
public class LivePushRunner implements ApplicationRunner {

    @Resource
    private Map<String, ScheduledPushServer> scheduledPushServerMap;

    @Resource
    private ScheduledExecutorService executorService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        scheduledPushServerMap.forEach((beanName,bean)->{
            executorService.scheduleAtFixedRate(bean, 0, bean.getScheduledTime(), bean.getTimeUnit());
        });

    }
}
